package com.heatup.apt.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.heatup.apt.api.controller.pay.util.PropertiesUtil;
import com.heatup.apt.api.service.CoachCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.common.dao.CoachCommentInfoMapper;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.common.dao.redis.TokenRedisDao;
import com.heatup.apt.common.util.WeiXinUtil;
import com.heatup.apt.model.coach.CoachComment;
import com.heatup.apt.model.weixin.Token;

/**
 *
 * CoachComment 表数据服务层接口实现类
 *
 */
@Service
public class CoachCommentServiceImpl extends SuperServiceImpl<CoachCommentInfoMapper, CoachComment> implements CoachCommentService {

    private static final Logger log = LoggerFactory.getLogger(CoachCommentServiceImpl.class);
    private static final String WECHAT_DOWNLOAD_URL= String.valueOf(PropertiesUtil.get("wechat_download_url")).trim();

    @Autowired
    private TokenMapper tokenMapper;
    @Autowired
    private CoachCommentInfoMapper coachCommentInfoMapper;
    @Autowired
    private TokenRedisDao tokenRedisDao;

    @Override
    public int insertCommentInfo(String comment, Integer userId, Integer courseId, String serviceIds, Integer coachUserId) {

        CoachComment commentInfo = new CoachComment();
        //获取上传到oss服务器的图片地址
//		String picArray = getImgFromWx(serviceIds);
        logger.info("serviceIds value is : " + serviceIds);
        commentInfo.setComment(comment);
        commentInfo.setUserId(userId);
        commentInfo.setCourseId(courseId);
        commentInfo.setCourseNumber(1);//TODO 请保存课时时序
        commentInfo.setCreatedBy(coachUserId);
        commentInfo.setCreatedTime(new Date());
        commentInfo.setCreatedBy(coachUserId);
        commentInfo.setCommentImgUrl(serviceIds);//图片地址 以#分隔
        logger.info("commentInfo value is : " + JSON.toJSONString(commentInfo));
        return coachCommentInfoMapper.insertSelective(commentInfo);
    }

    @Override
    public List<String> getImgFromWx(String serviceIds) {
        if(serviceIds == null) return null;
        String[] serId = serviceIds.split(",");
       // Token accssToken = tokenMapper.selectTokenByKey(TokenConstants.ACCESS_TOKEN_KEY);
        Token accssToken = tokenRedisDao.queryTokenByRedis();
        List<String> urls = new ArrayList<String>();
        try {
            for(String str : serId) {
                String downloadUrl = WECHAT_DOWNLOAD_URL.replace("#token#", accssToken.getToken()).replace("#serviceId#", str);
                String url = WeiXinUtil.uploadImgFromWxToOSS(downloadUrl);
                urls.add(url);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return urls;
    }
   
}